package tool

import (
	"errors"
	"fmt"
	"reflect"
	"service/model/mod"
	"strconv"
	"time"

	"gitee.com/cloudapex/ulern/util"
)

/*
 * 描述:
 *
 *******************************************************************/
type Personal struct {
	UId     int64  `json:"-"`         // 用户ID
	At      int64  `json:"-"`         // 数据的时间点
	ToCC    int64  `json:"per_tocc"`  // 拨打电话数量
	TQCall  int64  `json:"per_call"`  // 通话时长
	TQId    int64  `json:"per_tq_id"` // TQ 账号
	TQPass  string `json:"-"`         // TQ 密码
	TQToken string `json:"-"`         // TQ的Token
	TQToAt  int64  `json:"-"`         // 请求TQ Tkent的时间戳
	TQBill  string `json:"-"`         // 上次请求单号
	TQFage  int    `json:"-"`         // 0 首资 1 库存
}

func (this *Personal) RKey() string {
	return fmt.Sprintf("P:%.6d_H", this.UId)
}

func (this *Personal) RDb() mod.ERDB {
	return mod.ERDB_Permt
}

func (this *Personal) ToMap() map[interface{}]interface{} {
	m := make(map[interface{}]interface{})
	elem := reflect.ValueOf(this).Elem()
	relType := elem.Type()
	for i := 0; i < relType.NumField(); i++ {
		m[relType.Field(i).Name] = elem.Field(i).Interface()
	}
	return m
}
func (this *Personal) UsAdd(second int64) (interface{}, error) {
	fmt.Println(second)

	if err := mod.RDB(this.RDb()).Hash.HIncrBy(this.RKey(), "ToCC", 1).Error(); err != nil {
		return 0, err
	}
	return mod.RDB(this.RDb()).Hash.HIncrBy(this.RKey(), "TQCall", second).Result()

}

func (this *Personal) MapTo() error {
	//val, err := Ur(this.RDb()).HGetAll(this.RKey()).Result()

	val, err := mod.RDB(this.RDb()).Hash.HGetAll(this.RKey()).Result()
	v, ok := val.(map[string]string)
	if err == nil {
		if !ok {
			return nil
		}

		this.TQId, _ = strconv.ParseInt(v["TQId"], 10, 64)
		this.At, _ = strconv.ParseInt(v["At"], 10, 64)
		this.ToCC, _ = strconv.ParseInt(v["ToCC"], 10, 64)
		this.TQCall, _ = strconv.ParseInt(v["TQCall"], 10, 64)
		this.TQToAt, _ = strconv.ParseInt(v["TQToAt"], 10, 64)
		this.TQFage, _ = strconv.Atoi(v["TQFage"])
		this.TQToken = v["TQToken"]
		this.TQPass = v["TQPass"]
		this.TQBill = v["TQBill"]
	}
	return err
}

//func CallInfo(uid int64, tid string) int64 {
//	println("测试协程")
//	var val Personal
//	if tid == "" {
//		return 0
//	}
//	var call modes.CallPhone
//	call.UId = uid
//	call.TQBill = tid
//	if _, err := call.Get(); err != nil {
//		fmt.Println(err.Error())
//		return 0
//	}
//	tq := lib.UseTQ()
//	if err := tq.BillNoInfo(tid, &call); err != nil {
//		fmt.Println("aaaaaa", err.Error())
//		return 0
//	}
//
//	val.UId = val.UId
//	val.SetTQCall(call.Duratio)
//	return call.Duratio
//}

/*
 * 描述: 拨打电话
 *
 ************************************************************************************/
func (this *Personal) TQCallPhone(phone string, db int) (string, error) {
	err := this.MapTo()
	if err == nil {
		//go CallInfo(this.UId, this.TQBill)
		var fage bool = false
		tq := UseTQ()
		now_at := time.Now().Unix()
		if (this.TQToAt + 7200) < now_at {
			token, err := tq.GetPToken(this.TQId, this.TQPass)
			if err != nil {
				return "", err
			}
			this.TQToken = token
			this.TQToAt = now_at
			fage = true
		}
		var bill_no QTBillNo
		str_bill_no, _ := bill_no.GetBillNo()
		fage, err := tq.CallPhone(str_bill_no, phone, this.TQToken, this.TQId)
		fmt.Println(fage)
		if !fage {
			return "", errors.New("TQ账号异常")
		}
		if fage && err == nil {
			if util.IsToday(this.At) {
				this.ToCC++
			} else {
				this.At = now_at
				this.ToCC = 1
			}
			fage = true
		}
		this.TQBill = str_bill_no
		this.TQFage = db
		if fage {
			this.Add()
		}
		return str_bill_no, err
	}
	return "", err
}

/*
 * 描述: 添加招生老师
 *
 ******************************************************************/
func (this *Personal) Add() error {

	return mod.RDB(this.RDb()).Hash.HMSetMap(this.RKey(), this.ToMap()).Error()
	//return Ur(this.RDb()).HMSet(this.RKey(), this.ToMap()).Err()
}

/*
 * 描述: 今日拨打数量
 *
 ******************************************************************/
//func (this *Personal) CallCount() error {
//	err := this.MapTo()
//	if err == nil {
//		if lib.IsToday(this.At) {
//			return nil
//		} else {
//			this.At = time.Now().Unix()
//			this.ToCC = 0
//			return this.Add()
//		}
//	}
//	return err
//}

/*
 * 描述: 添加招生老师
 *
 ******************************************************************/
//func (this *Personal) GetPId() ([]int64, error) {
//	var val []int64
//	mp, err := Ur(this.RDb()).SMembers(fmt.Sprintf("P:%.06d_S", this.UId)).Result()
//	if err == nil {
//		for _, v := range mp {
//			n, _ := strconv.ParseInt(v, 10, 64)
//			val = append(val, n)
//		}
//	}
//	return val, err
//}
//
//func (this *Personal) SetTQCall(second int64) error {
//	return Ur(this.RDb()).HIncrBy(this.RKey(), "TQCall", second).Err()
//}
